package com.juyu.reader.author.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.author.common.Configure;
import com.juyu.reader.author.model.Page;
import com.juyu.reader.author.util.StringUtil;
import com.juyu.reader.author.model.Author;

public class BaseController {
	private int count;
	private Page pager = new Page();
	private String message;
	private String statusCode;

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public Author getSessionAuthor(HttpServletRequest request) {
		Author author = (Author) request.getSession().getAttribute(Configure.systemSessionAuthor);
		return author;
	}

	public ModelAndView packModeAndView(HttpServletRequest request, String jumpView, String message,
			Map<String, Object> attrMap) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(jumpView);
		modelAndView.addObject("message", message == null ? "" : message);
		modelAndView.addObject("path", request.getContextPath());
		if (null != attrMap) {
			for (String key : attrMap.keySet()) {
				modelAndView.addObject(key, attrMap.get(key));
			}
		}
		return modelAndView;
	}

	public String packStringView(HttpServletRequest request, String jumpView, String message, Model model) {
		model.addAttribute("message", message == null ? "" : message);
		model.addAttribute("path", request.getContextPath());
		return jumpView;
	}

	public int getLastPageNo(HttpServletRequest request) {
		String lastPageNo = request.getParameter("lastPageNo");
		if (StringUtil.isNullOrEmpty(lastPageNo)) {
			return 1;
		} else {
			int pageNo = 1;
			try {
				pageNo = Integer.valueOf(lastPageNo);
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (pageNo <= 0) {
				pageNo = 1;
			}
			return pageNo;
		}

	}
	// @ExceptionHandler(Exception.class)
	// public String exceptionHandler(HttpServletRequest request,
	// HttpServletResponse response, Exception e) {
	//
	// System.out.println(e.toString());
	// if (e instanceof MaxUploadSizeExceededException) {
	// this.setMessage("文件应不大于 " +
	// FileUtil.getFileMB(((MaxUploadSizeExceededException)
	// e).getMaxUploadSize()));
	// } else {
	// this.setMessage("文件上传失败，未知错误！");
	// }
	// return null;
	// }
}
